/*   1:    */ package com.atomikos.beans;
/*   2:    */ 
/*   3:    */ import com.atomikos.util.ClassLoadingHelper;
/*   4:    */ import java.io.PrintWriter;
/*   5:    */ import java.util.ArrayList;
/*   6:    */ import java.util.Collection;
/*   7:    */ import java.util.Enumeration;
/*   8:    */ import java.util.HashMap;
/*   9:    */ import java.util.Map;
/*  10:    */ import javax.servlet.ServletException;
/*  11:    */ import javax.servlet.http.HttpServletRequest;
/*  12:    */ import javax.servlet.http.HttpServletResponse;
/*  13:    */ 
/*  14:    */ public class HtmlBeanWizard
/*  15:    */ {
/*  16:    */   private static final String PAR_NAME_PREFIX = "com.atomikos.beans.property.";
/*  17:    */   private static final String BOOLEAN_PREFIX = "boolean.";
/*  18:    */   private BeanInspector inspector;
/*  19:    */   private Map parsedProperties;
/*  20:    */   
/*  21:    */   private static Property[] filterProperties(Property[] props)
/*  22:    */     throws PropertyException
/*  23:    */   {
/*  24: 64 */     if (props == null) {
/*  25: 64 */       return null;
/*  26:    */     }
/*  27: 66 */     ArrayList list = new ArrayList();
/*  28: 68 */     for (int i = 0; i < props.length; i++) {
/*  29: 69 */       if ((!props[i].isHidden()) && (!props[i].isReadOnly()) && (props[i].getEditor() != null) && (props[i].getIndexedProperty() == null)) {
/*  30: 73 */         list.add(props[i]);
/*  31:    */       }
/*  32:    */     }
/*  33: 76 */     return (Property[])list.toArray(new Property[0]);
/*  34:    */   }
/*  35:    */   
/*  36:    */   public HtmlBeanWizard()
/*  37:    */   {
/*  38: 87 */     this.parsedProperties = new HashMap();
/*  39:    */   }
/*  40:    */   
/*  41:    */   public void setClassName(String name)
/*  42:    */     throws ClassNotFoundException
/*  43:    */   {
/*  44:101 */     Class clazz = ClassLoadingHelper.loadClass(name);
/*  45:    */     
/*  46:103 */     Object bean = null;
/*  47:    */     try
/*  48:    */     {
/*  49:105 */       bean = clazz.newInstance();
/*  50:    */     }
/*  51:    */     catch (Exception e)
/*  52:    */     {
/*  53:108 */       throw new RuntimeException(e.getMessage());
/*  54:    */     }
/*  55:110 */     this.inspector = new BeanInspector(bean);
/*  56:    */   }
/*  57:    */   
/*  58:    */   public String[] getPropertyNames()
/*  59:    */     throws PropertyException
/*  60:    */   {
/*  61:117 */     Collection names = new ArrayList();
/*  62:118 */     String[] template = new String[0];
/*  63:120 */     if (this.inspector == null) {
/*  64:120 */       return template;
/*  65:    */     }
/*  66:122 */     Property[] props = filterProperties(this.inspector.getProperties());
/*  67:123 */     for (int i = 0; i < props.length; i++) {
/*  68:124 */       names.add(props[i].getName());
/*  69:    */     }
/*  70:126 */     return (String[])names.toArray(template);
/*  71:    */   }
/*  72:    */   
/*  73:    */   public Object getPropertyValue(String propertyName)
/*  74:    */     throws PropertyException
/*  75:    */   {
/*  76:132 */     Object ret = null;
/*  77:134 */     if (this.parsedProperties.containsKey(propertyName))
/*  78:    */     {
/*  79:138 */       ret = this.parsedProperties.get(propertyName);
/*  80:    */     }
/*  81:140 */     else if (this.inspector != null)
/*  82:    */     {
/*  83:141 */       Property p = this.inspector.getProperty(propertyName);
/*  84:    */       try
/*  85:    */       {
/*  86:144 */         ret = p.getValue();
/*  87:    */       }
/*  88:    */       catch (PropertyException readOnly) {}
/*  89:    */     }
/*  90:150 */     return ret;
/*  91:    */   }
/*  92:    */   
/*  93:    */   public void showPropertiesInForm(HttpServletResponse response)
/*  94:    */   {
/*  95:162 */     if (this.inspector == null) {
/*  96:162 */       return;
/*  97:    */     }
/*  98:    */     try
/*  99:    */     {
/* 100:165 */       PrintWriter out = response.getWriter();
/* 101:166 */       Property[] props = filterProperties(this.inspector.getProperties());
/* 102:167 */       out.println("<table>");
/* 103:168 */       for (int i = 0; i < props.length; i++)
/* 104:    */       {
/* 105:170 */         out.println("<tr>");
/* 106:    */         
/* 107:172 */         String name = props[i].getName();
/* 108:173 */         out.println("<td>" + name + "</td>");
/* 109:174 */         out.println("<td>");
/* 110:175 */         Class clazz = PrimitiveClasses.getWrapperClass(props[i].getType());
/* 111:176 */         if (props[i].getAllowedValues() != null)
/* 112:    */         {
/* 113:177 */           out.println("<select name=\"com.atomikos.beans.property." + name + "\" >");
/* 114:    */           
/* 115:179 */           String[] values = props[i].getAllowedValues();
/* 116:180 */           for (int j = 0; j < values.length; j++) {
/* 117:181 */             out.println("<option value=\"" + values[j] + "\">" + values[j]);
/* 118:    */           }
/* 119:183 */           out.println("</select>");
/* 120:    */         }
/* 121:186 */         else if (Boolean.class.equals(clazz))
/* 122:    */         {
/* 123:188 */           out.println("<input type=\"checkbox\" value=\"true\" name=\"com.atomikos.beans.property.boolean." + name + "\"");
/* 124:    */           try
/* 125:    */           {
/* 126:192 */             Boolean b = (Boolean)props[i].getValue();
/* 127:194 */             if (b.booleanValue()) {
/* 128:194 */               out.println(" checked ");
/* 129:    */             }
/* 130:    */           }
/* 131:    */           catch (Exception ignore) {}
/* 132:202 */           out.println(">");
/* 133:    */         }
/* 134:    */         else
/* 135:    */         {
/* 136:205 */           out.print("<input type=\"text\" name=\"com.atomikos.beans.property." + name + "\" value=\"");
/* 137:    */           try
/* 138:    */           {
/* 139:209 */             Object value = props[i].getValue();
/* 140:210 */             out.print(value.toString());
/* 141:    */           }
/* 142:    */           catch (NullPointerException noValue) {}catch (Exception ignore) {}
/* 143:222 */           out.println("\" >");
/* 144:    */         }
/* 145:225 */         out.println("</td>");
/* 146:226 */         out.println("</tr>");
/* 147:    */       }
/* 148:228 */       out.println("</table>");
/* 149:    */     }
/* 150:    */     catch (Exception e)
/* 151:    */     {
/* 152:232 */       e.printStackTrace();
/* 153:    */     }
/* 154:    */   }
/* 155:    */   
/* 156:    */   public void parseProperties(HttpServletRequest request)
/* 157:    */     throws ServletException, ReadOnlyException
/* 158:    */   {
/* 159:249 */     if (this.inspector == null) {
/* 160:249 */       return;
/* 161:    */     }
/* 162:250 */     Enumeration pars = request.getParameterNames();
/* 163:    */     
/* 164:252 */     this.parsedProperties = new HashMap();
/* 165:254 */     while (pars.hasMoreElements())
/* 166:    */     {
/* 167:255 */       boolean bool = false;
/* 168:256 */       String name = (String)pars.nextElement();
/* 169:257 */       if (name.startsWith("com.atomikos.beans.property."))
/* 170:    */       {
/* 171:258 */         String propertyName = name.substring("com.atomikos.beans.property.".length());
/* 172:    */         try
/* 173:    */         {
/* 174:262 */           if (propertyName.startsWith("boolean."))
/* 175:    */           {
/* 176:263 */             bool = true;
/* 177:264 */             propertyName = propertyName.substring("boolean.".length());
/* 178:    */           }
/* 179:267 */           if ((request.getParameter(name) != null) && (!request.getParameter(name).equals("")))
/* 180:    */           {
/* 181:269 */             if (!bool)
/* 182:    */             {
/* 183:270 */               setProperty(propertyName, request.getParameter(name));
/* 184:271 */               this.parsedProperties.put(propertyName, request.getParameter(name));
/* 185:    */             }
/* 186:    */             else
/* 187:    */             {
/* 188:275 */               setProperty(propertyName, "true");
/* 189:276 */               this.parsedProperties.put(propertyName, new Boolean(true));
/* 190:    */             }
/* 191:    */           }
/* 192:279 */           else if (bool)
/* 193:    */           {
/* 194:281 */             setProperty(propertyName, "false");
/* 195:282 */             this.parsedProperties.put(propertyName, new Boolean(false));
/* 196:    */           }
/* 197:    */         }
/* 198:    */         catch (Exception e1)
/* 199:    */         {
/* 200:289 */           e1.printStackTrace();
/* 201:    */         }
/* 202:    */       }
/* 203:    */     }
/* 204:    */   }
/* 205:    */   
/* 206:    */   public void setProperty(String name, String value)
/* 207:    */     throws ReadOnlyException, PropertyException
/* 208:    */   {
/* 209:299 */     if (this.inspector == null) {
/* 210:300 */       throw new IllegalStateException("Not initialized");
/* 211:    */     }
/* 212:302 */     Property p = this.inspector.getProperty(name);
/* 213:303 */     p.getEditor().setStringValue(value);
/* 214:304 */     p.setValue(p.getEditor().getEditedObject());
/* 215:    */   }
/* 216:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\atomikos-util\3.7.0\atomikos-util-3.7.0.jar
 * Qualified Name:     com.atomikos.beans.HtmlBeanWizard
 * JD-Core Version:    0.7.0.1
 */